'use strict';
var fs = require('fs');
var path = require('path');
var webpack = require('webpack');
var openBrowserPlugin = require('open-browser-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var ROOT_PATH = path.resolve(__dirname);
var APP_PATH = path.resolve(ROOT_PATH, './app');
var LIB_PATH = path.resolve(ROOT_PATH, './lib');
//var BUILD_PATH = path.resolve(ROOT_PATH, '../../../../../../../dz-boss-pro');
var BUILD_PATH = path.resolve(ROOT_PATH, './build');
var MODULES_PATH = path.resolve(ROOT_PATH, './node_modules');
var DEFAULT = path.resolve(APP_PATH, 'default');
var HtmlwebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");


var plugins = [
    new webpack.DefinePlugin({
        'process.env': {
            NODE_ENV: '"development"'
        }
    }),

    new webpack.ContextReplacementPlugin(
        /moment[\\\/]locale$/,
        /^\.\/(zh-cn)$/
        ///(zh-cn)\.js/
    ),

    new webpack.ProvidePlugin({
        Axios: "axios",
        React: "react",
        ReactDOM: 'react-dom',
        Router: ["react-router", "Router"],
        Route: ["react-router", "Route"],
        Link: ["react-router", "Link"],
        ClassNames: ["classnames"],
        PropTypes: ["prop-types"],
        Immer: ["immer", "default"],
        Connect: ["react-redux", "connect"],
        ActionTypes: "@/redux/action-types",
        Actions: ["@/redux/actions/index", "default"],
        Invoke: ["@/invoke/index", "default"]
    }),

    new webpack.NamedModulesPlugin(),

    new webpack.optimize.CommonsChunkPlugin({
        name: 'common',
        filename: 'js/common-[hash:6].js',
        minChunks: 3
    }),


    new openBrowserPlugin({ url: 'http://dev.weizhipin.com:8080/html/index.html' }),

]

var entris = fs.readdirSync(APP_PATH).reduce(function (o, filename) {
    if (/\.js/g.test(filename)) {
        var _name = filename.replace(/\.js$|\.jsx$/g, '');
        //o[_name] = ["react-hot-loader/patch", path.resolve(APP_PATH, filename)];
        o[_name] = path.resolve(APP_PATH, filename);
        var html = new HtmlwebpackPlugin({
            title: '网站管理后台',
            filename: _name + '.html',
            chunks: ["common", _name],
            favicon: path.resolve(DEFAULT, 'favicon.ico'),
            hash: false,
            template: path.resolve(DEFAULT, 'default-template.html')
        });
        plugins.push(html);
    }
    return o;
}, { "common": ['babel-polyfill', "react", "react-dom", "react-router"] });

module.exports = {
    devtool: 'cheap-module-eval-source-map',
    module: {
        rules: [
            {
                test: /\.(js|jsx)?$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        babelrc: false,
                        presets: ['env', 'react', 'stage-0'],
                        plugins: ["transform-remove-strict-mode"],
                        //, plugins: ["react-hot-loader/babel"]   //react-hot-loader/patch 不写记录不到状态
                    }
                }
            },
            {
                test: /\.scss$/,
                use: ['style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            sourceMap: true,
                            //modules: true,
                        }
                    },
                    'postcss-loader',
                    'sass-loader'
                ]
            },
            {
                test: /\.css$/,
                use: ['style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            sourceMap: true,
                            //modules: true,
                        }
                    },
                    'postcss-loader'
                ]
            },
            {
                test: /\.(png|jpeg|jpg|gif)$/,
                use: {
                    loader: "url-loader",
                    options: {
                        limit: "20000",
                        name: "img/[name]-[hash:6].[ext]"
                    }
                }
            }
        ]
    },

    entry: entris,

    output: {
        filename: 'js/[name]-[hash:6].js',
        chunkFilename: 'js/[name]-[hash:6].js',
        path: BUILD_PATH,
        //publicPath: '/html/'
    },

    devServer: {
        compress: true,
        hot: true,
        inline: true,
        contentBase: "./app/",
        publicPath: "/html/",
        host: '0.0.0.0',
        disableHostCheck: true,
        //headers: {
        //    "Access-Control-Allow-Origin": "http://dev.weizhipin.com:8080"
        //},
        //historyApiFallback: true,
        proxy: {
            "/api": {
                //target: "https://blue-boss.weizhipin.com/",
                target: "http://127.0.0.1/",
                pathRewrite: { "^/api": "" },
                secure: false,
                changeOrigin: true,
                //cookieDomainRewrite: {
                //    "dev.weizhipin.com": "blue-boss.weizhipin.com"
                //}
            }
        }
    },

    resolve: {
        extensions: ['.js', '.jsx', '.json', 'css'],
        modules: ["node_modules", path.resolve(__dirname, 'app')],
        alias: {
            '@': path.resolve(__dirname, 'app'),
            'react': path.resolve(MODULES_PATH, './react'),
            'react-dom': path.resolve(MODULES_PATH, './react-dom'),
            'my-default-style': path.resolve(DEFAULT, './default-style.css'),
            'base': path.resolve(LIB_PATH, './base.js'),
            'vali': path.resolve(LIB_PATH, './vali.js'),
            'api': path.resolve(LIB_PATH, './api.js')
        }
    },

    plugins: plugins

};